/*
 *  levelUpGame - BackgroundView.cpp
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#include "ofxAlphaVideoPlayer.h"
#include "LevelUpApp.h"
#include "BackgroundView.h"

//--------------------------------------------------------------
BackgroundView::BackgroundView( const LevelUpApp& appRef_ )
: s( Settings::getInstance() ), appRef( appRef_ ), game( appRef_.getGame() ),
  bgVideo( 0 ), facadeVideo( 0 )
{}

//--------------------------------------------------------------
BackgroundView::~BackgroundView() {
	delete bgVideo;
	delete facadeVideo;
}


//--------------------------------------------------------------
void BackgroundView::setup() {
	const string* bgMovieFile = s.getValue( "background_video_file", "" );
	const string* facadeMovieFile = s.getValue( "facade_video", "" );
	bgVideoDisabled = parseBool( *s.getValue( "bg_video_disabled", "false" ) );
	facadeVideoDisabled = parseBool( *s.getValue( "facade_video_disabled", "false" ) );
	bgVideoHasMirrorCopy = parseBool( *s.getValueInGroup( "bg_video_has_mirror_copy", "resource", "false" ) );
	facadeVideoHasMirrorCopy = parseBool( *s.getValueInGroup( "facade_video_has_mirror_copy", "resource", "false" ) );
	
	if ( ! bgMovieFile ) {
		ofLog( OF_LOG_FATAL_ERROR, "(BackgroundView.cpp) config key 'background_video_file' could not be found" );
		return;
	}
	
	if ( ! facadeMovieFile ) {
		ofLog( OF_LOG_FATAL_ERROR, "(BackgroundView.cpp) config key 'facade_video' could not be found" );
		return;
	}	
	
	float balance = game.getBalanceMeter().getBalanceFloat() + 0.5f;
	
	if ( ! bgVideoDisabled ) {
		bgVideo = new ofxAlphaVideoPlayer();
		bool rv = bgVideo->loadMovie( bgMovieFile->c_str() );
		if ( rv ) {
			bgVideo->setLoopState( OF_LOOP_NORMAL );
			bgVideo->setPosition( ( ! bgVideoHasMirrorCopy ) ? balance : balance / 2.0f );
		} else {
			ofLog( OF_LOG_FATAL_ERROR, "(BackgroundView.cpp) could not load background video file '%s'", bgMovieFile->c_str() );
			delete bgVideo; bgVideo = 0;
		}
	}
	
	if ( ! facadeVideoDisabled ) {
		facadeVideo = new ofxAlphaVideoPlayer();
		bool rv = facadeVideo->loadMovie( facadeMovieFile->c_str() );
		if ( rv ) {
			facadeVideo->setLoopState( OF_LOOP_NORMAL );
			facadeVideo->setPosition( ( ! facadeVideoHasMirrorCopy ) ? balance : balance / 2.0f );
		} else {
			ofLog( OF_LOG_FATAL_ERROR, "(BackgroundView.cpp) could not load facade video file '%s'", facadeMovieFile->c_str() );
			delete facadeVideo; facadeVideo = 0;
		}
	}
	
	if ( bgVideo ) bgVideo->play();
	if ( facadeVideo ) facadeVideo->play();
}

//--------------------------------------------------------------
void BackgroundView::update() {
	if ( bgVideo ) bgVideo->idleMovie();
	if ( facadeVideo ) facadeVideo->idleMovie();
}

//--------------------------------------------------------------
void BackgroundView::draw() {
	//TEMP
	ofEnableAlphaBlending();
	//ofSetColor( 100, 255, 100 );
	ofSetColor( 0,0,0 );
	ofRect( 0, 0, 1400, 1050 );
	ofSetColor( 255, 255, 255 );

	ofPushStyle();
	ofSetLineWidth( 3 );
	float width = 160, hHeight = 50;
	
	if ( bgVideo ) {
		bgVideo->draw( 0, 0 );
		
		if ( appRef.debugDrawingEnabled ) {
			ofPoint bgRange( game.getColumnLanes()[0] + 30, game.getRowLanes()[game.getRowLanes().size() - 1] + 100 );
			float bgPosX = ofMap( getBackgroundPosition(), 0.0f, 1.0f, bgRange.x, bgRange.x + width, false );
			ofSetColor( 63, 255, 255 ); ofLine( bgPosX, bgRange.y - hHeight, bgPosX, bgRange.y + hHeight );
			ofSetColor( 255, 255, 255 ); ofLine( bgRange.x, bgRange.y, bgRange.x + width, bgRange.y );
		}
	}
	
	if ( facadeVideo ) {
		facadeVideo->draw( 0, 0 );
		
		if ( appRef.debugDrawingEnabled ) {
			ofPoint fcRange( game.getColumnLanes()[0] + 30, game.getRowLanes()[game.getRowLanes().size() - 1] + 150 );
			float fcPosX = ofMap( getFacadePosition(), 0.0f, 1.0f, fcRange.x, fcRange.x + width, false );
			ofSetColor( 255, 255, 63 ); ofLine( fcPosX, fcRange.y - hHeight, fcPosX, fcRange.y + hHeight );
			ofSetColor( 255, 255, 255 ); ofLine( fcRange.x, fcRange.y, fcRange.x + width, fcRange.y );
		}
	}
	
	ofPopStyle();
}

float BackgroundView::getBackgroundPosition() const { return ( bgVideo ) ? bgVideo->getPosition() : 0;  }
float BackgroundView::getFacadePosition() const { return ( facadeVideo ) ? facadeVideo->getPosition() : 0; }

void BackgroundView::setBackgroundPosition( float pos_ ) { if ( bgVideo ) bgVideo->setPosition( pos_ ); }
void BackgroundView::setFacadePosition( float pos_ ) { if ( facadeVideo ) facadeVideo->setPosition( pos_ ); }

void BackgroundView::setBackgroundSpeed( float speed_ ) { if ( bgVideo ) bgVideo->setSpeed( speed_ ); }
void BackgroundView::setFacadeSpeed( float speed_ ) { if ( facadeVideo ) facadeVideo->setSpeed( speed_ ); }
